<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Services_Module
{
	const EOL = "\n";
	
	public static function moduleSelect($attributes = array())
	{
		$json = new Services_JSON();
		$view = Gio_Core_View::getInstance();
		
		$selectedId = isset($attributes['selected']) ? $attributes['selected'] : null;
		$disableId  = isset($attributes['disable']) ? $attributes['disable'] : null;
		
		$elementDisabled = (isset($attributes['disabled']) && $attributes['disabled'] === true)
								? ' disabled="disabled"' : '';
		
		$output 	= sprintf("<select name='%s' id='%s' viewHelperClass='%s' viewHelperAttributes='%s'%s>", 
							$attributes['name'],
							$attributes['id'],
							'Modules_Core_Services_Module',
							$json->encodeUnsafe($attributes),
							$elementDisabled) . self::EOL
					. '<option value="">---</option>' . self::EOL;
		$modules = self::getModulesInstalled();		
		foreach ($modules as $module) {
			$selected = ($selectedId == null || $selectedId != $module['module_id']) ? '' : ' selected="selected"';
			$disable  = ($disableId == null || $disableId != $module['module_id']) ? '' : ' disabled';
			$output  .= sprintf('<option value="%s"%s%s>%s</option>',
								$module['module_id'],
								$selected,
								$disable,
								$view->TRANSLATOR->translator('about_' . $module['description'] . '_title', $module['module_id'])) . self::EOL;
		}
		$output .= '</select>' . self::EOL;

		return $output;
	}
	
	public static function getById($id)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$moduleDao = new Modules_Core_Models_Mysql_Module();
		$moduleDao->setConnection($conn);
		return $moduleDao->getById($id);
	}
	
	public static function getModulesInstalled()
	{
		$conn		= Gio_Db_Connection::getConnection();
		$moduleDao = new Modules_Core_Models_Mysql_Module();
		$moduleDao->setConnection($conn);
		return $moduleDao->getModulesInstalled();
	}
	
	public static function add($module)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$moduleDao = new Modules_Core_Models_Mysql_Module();
		$moduleDao->setConnection($conn);
		return $moduleDao->add($module);
	}
	
	public static function delete($moduleId)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$moduleDao = new Modules_Core_Models_Mysql_Module();
		$moduleDao->setConnection($conn);
		return $moduleDao->delete($moduleId);
	}
		
	public static function getActions($module) 
	{
		$file 	   = ROOT_DIR . DS . 'modules' . DS . $module . DS . 'configs' . DS . 'permissions.xml';
		if (!file_exists($file)) {
			return null;
		}
		$ret  	   = array();
		$xml  	   = @simplexml_load_file($file);
		$resources = $xml->controller;
		
		foreach ($resources as $res) {
			$langKey     = (string) $res->attributes()->langKey;
			$description = $langKey;
			$description = (null == $description) ? (string) $res->attributes()->description : $description;
			$controller    = array(
								'description' 	  => $description,
								'module_name' 	  => $module,
								'controller_name' => (string) $res->attributes()->name,
							);
			$privileges  = array();
			
			if ($res->action) {
				$actions = array();
				foreach ($res->action as $pri) {
					$langKey      = (string) $pri->attributes()->langKey;
					$description  = $langKey;
					$description  = (null == $description) ? (string) $pri->attributes()->description : $description;
					$actions[] = array(
										'name' 			  => (string) $pri->attributes()->name,
										'description' 	  => $description,
										'module_name'	  => $module,
										'controller_name' => (string) $res->attributes()->name,
								);
				}
			}
			$ret[] = array('controller' => $controller, 'actions' => $actions);
		}
		
		return $ret;
	}
	
	public static function install($module)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$moduleDao = new Modules_Core_Models_Mysql_Module();
		$moduleDao->setConnection($conn);
		return $moduleDao->install($module);
	}
	
	public static function uninstall($module)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$moduleDao = new Modules_Core_Models_Mysql_Module();
		$moduleDao->setConnection($conn);
		return $moduleDao->uninstall($module);
	}
}